// tabble gallery
document.querySelectorAll('table:has(img)').forEach(item => {
    item.classList.add('nexmoe-album');
});

// search
function search() {
    window.open(
        document.querySelector('#search_form').getAttribute('action_e')
			+ ' '
			+ document.querySelector('#search_value').value
    );
    return false;
}

// catalog
document.querySelectorAll('a.toc-link').forEach(item => {
    item.addEventListener('click', function(ev) {
        ev.preventDefault();
        const title = this.getAttribute('href');
        window.scroll({
            top: document.querySelector(decodeURI(title)).offsetTop - 24,
            behavior: 'smooth'
        });
    });
});

document.addEventListener('copy', () => {
    if (!window.copyTip) {
        return;
    }
    const sel = document.getSelection();
    const ele = document.createElement('div');
    ele.innerHTML
		= '<div style="position: fixed;opacity: 0;white-space: pre;">'
		+ sel
		+ '\n\n'
		+ window.copyTip.replaceAll('%url', document.location.href)
		+ ' </div>';
    document.body.appendChild(ele);
    sel.selectAllChildren(ele);
    setTimeout(() => {
        document.body.removeChild(ele);
    });
});

function imgOnError(_this) {
    _this.src = '';
    _this.onerror = null;
}

document
    .querySelectorAll('.nexmoe-post-cover.absolute')
    .forEach(item => {
        item.style.minHeight = item.childNodes[3].clientHeight + 'px';
    });

window.onload = function() {
    lax.init();

    // Add a driver that we use to control our animations
    lax.addDriver('scrollY', () => {
        return window.scrollY;
    });

    // Add animation bindings to elements
    lax.addElements('.backtop', {
        scrollY: {
            opacity: [
                ['screenHeight', 'screenHeight+300', 'screenHeight+600'],
                [0, 0, 1]
            ]
        }
    });

    lax.addElements('.nexmoe-post-cover', {
        scrollY: {
            opacity: [
                ['elInY', 'elInY+200'],
                [0, 1]
            ]
        }
    });
};
